package com.erdos.upc.web.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.erdos.common.web.LoginUtil;
import com.erdos.upc.core.UpcConst;
import com.erdos.upc.core.UpcUser;
import com.erdos.upc.core.UpcUtil;


public class SessionUpcFilter implements Filter {
	
	private static final Log logger = LogFactory.getLog(SessionUpcFilter.class);

	/* (non-Javadoc)
	 * @see javax.servlet.Filter#destroy()
	 */
	public void destroy() {
		
	}

	/* (non-Javadoc)
	 * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpSession session = ((HttpServletRequest)request).getSession();
		UpcUser upcUser=(UpcUser)session.getAttribute(UpcConst.LOGIN_UPC_USER);
		if(upcUser!=null){
			LoginUtil.setUpcUserPin(upcUser.getLoginId());
			//LoginUtil.setUpcUserGroup(upcUser.getLoginGroup());
			LoginUtil.setFacilityNo(upcUser.getFacilityNo());
		}
		logger.debug("SessionUpcFilter UpcUtil.getUpcUserPin()="+UpcUtil.getUpcUserPin());
        try {
            chain.doFilter(request, response);
        } finally {
        	LoginUtil.setUpcUserPin(null);
        	//LoginUtil.setUpcUserGroup(null);
		LoginUtil.setFacilityNo(null);
        }
        logger.debug("User Filter end!");
	}

	/* (non-Javadoc)
	 * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
	 */
	public void init(FilterConfig arg0) throws ServletException {
		
	}

}
